package com.by.yz.mypersonal;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.entity.Account;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by John on 2017/3/23.
 */
public class PersonalImproveInfoActivity  extends BaseActivity{
    private EditText registerEduction;
    private EditText registerEmail;
    private EditText registerUserresume;
    private EditText registerHistory;
    private Button finish,ignore;
    private UserTest user;
    private Account account;
    private String eduction,email,userresume,history,username;
    private TextView tishi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_more);
        setToolbarTitle("更多资料");
        Intent intent=getIntent();
        username=intent.getStringExtra("username").toString();
        Log.e("username = ",username);
        user=(UserTest) intent.getSerializableExtra("user");
        boolean enableUpdate = intent.getBooleanExtra("setting", false);

        registerEduction=(EditText)findViewById(R.id.personal_eduction);
        registerEmail=(EditText)findViewById(R.id.personal_email);
        registerHistory=(EditText)findViewById(R.id.personal_history);
        registerUserresume=(EditText)findViewById(R.id.personal_userresume);
        tishi = (TextView) findViewById(R.id.personal_tishi);

        registerEduction.setText(user.getEducation());
        registerEmail.setText(user.getEmail());
        registerHistory.setText(user.getHistory());
        registerUserresume.setText(user.getUserresume());

        finish = (Button) findViewById(R.id.personal_finish);
        if(!enableUpdate){
            registerEduction.setKeyListener(null);
            registerEduction.setFocusable(false);
            registerEduction.setFocusableInTouchMode(false);

            registerEmail.setKeyListener(null);
            registerEmail.setFocusable(false);
            registerEmail.setFocusableInTouchMode(false);

            registerHistory.setKeyListener(null);
            registerHistory.setFocusable(false);
            registerHistory.setFocusableInTouchMode(false);

            registerUserresume.setKeyListener(null);
            registerUserresume.setFocusable(false);
            registerUserresume.setFocusableInTouchMode(false);

            tishi.setVisibility(View.GONE);
            finish.setVisibility(View.GONE);
        }
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getUser();
            }
        });
    }

    public void getUser(){
        if(isNetworkAvailable(PersonalImproveInfoActivity.this)){
                    eduction=registerEduction.getText().toString();
                    history=registerHistory.getText().toString();
                    email=registerEmail.getText().toString();
                    userresume=registerUserresume.getText().toString();
                    //获取用户信息
                    UserRESTFul userRESTFul=new UserRESTFul();
                    userRESTFul.getUser(username, new UserRESTFul.UserPOSTCallBack() {
                        @Override
                        public void ok(JSONObject response) throws JSONException {
                            JSONObject jsonObject=response;
                            Log.e("hahahahh``","获取用户表成功"+response);
                            //user=ToJSonObject.jsonToBean(jsonObject.toString(),UserTest.class);
                            user.setEmail(email);
                            user.setUserresume(userresume);
                            user.setEducation(eduction);
                            user.setHistory(history);
                            updateUser();
                        }
                        @Override
                        public void error(String response) {
                            Log.e("hahahahh","获取用户表失败"+response);
                        }
                    });
        }else{
            //无网络连接情况
            new AlertDialog.Builder(PersonalImproveInfoActivity.this)
                    .setTitle("提示!")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setMessage("检测到您还没开启网络，请开启")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("开启",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    startActivity(new Intent(
                                            Settings.ACTION_WIRELESS_SETTINGS));// 进入无线网络配置界面
                                }
                            }).show();
        }

    }

    public void updateUser(){
        JSONObject data = ToJSonObject.beanToJSONObejct(user);
        UserRESTFul userRESTFul=new UserRESTFul();
        userRESTFul.putUser(data, new UserRESTFul.UserPOSTCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                Intent intent=new Intent();
                Toast.makeText(getApplicationContext(), "信息修改成功", Toast.LENGTH_SHORT).show();
//                Handler handler = new Handler();
//                handler.postDelayed(new Runnable() {
//                    public void run() {
//                        MFGT.finish(PersonalImproveInfoActivity.this);
//                    }
//                },2000);
            }
            @Override
            public void error(String response) {
                Toast.makeText(getApplicationContext(), "信息完善失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
        });
    }
    /*
* 判断是否有网络
*/
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return false;
        } else {
            // 如果仅仅是用来判断网络连接　　　　　　
            // 则可以使用 cm.getActiveNetworkInfo().isAvailable();
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            if (networkInfo != null) {
                Log.e("hj","isNetworkAvailable - 是否有网络： "+ networkInfo.isAvailable());
                return true;
            } else {
                Log.e("hj", "isNetworkAvailable - 完全没有网络！");
                return false;
            }
        }
    }
}
